package com.zq.it.service.impl.admin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.zq.it.pojo.Academy;
import com.zq.it.pojo.Major;
import com.zq.it.service.admin.MajorService;
import com.zq.it.service.common.BaseServiceImpl;
import com.zq.it.tool.HeadTitle;

public class MajorServiceImpl extends BaseServiceImpl implements MajorService {

	//private List<Major> ; //存储查询数据
	@Override
	public List<Major> getAllMajor() {
		
		// TODO Auto-generated method stub
		return getMajorDao().findMajorEtyList();
	}

	@Override
	public void removeMajor(Integer id) {
		getMajorDao().deleteMajorEtyByID(id);
		// TODO Auto-generated method stub
	
	}

	@Override
	public void renewalMajor(Major major) {
        getMajorDao().updateMajorEty(major);
	}

	@Override
	public Major getMajor(Integer id) {
		return getMajorDao().findMajorEty(id);
	}

	@Override
	public void addMajor(Major major){	
		getMajorDao().saveMajorEty(major);
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("deprecation")
	@Override
	public List getMajorListByType(String type,String parameter) {
		List list=null;
		if(type.equals("aId"))
			list=getMajorDao().findMajorEtyById(Integer.parseInt(parameter));
		if(type.equals("aNa"))
			list=getMajorDao().findMajorEtyByName(parameter);
		if(type.equals("aDa"))
			list=getMajorDao().findMajorEtyByDate(com.zq.it.tool.Constant.getDateFromStr(parameter));	
		if(type.equals("aCmy"))
			list=getMajorDao().findMajorEtyByAcmyId(Integer.parseInt(parameter));
		return list ;
	}

	@Override
	public List getMajorByExmp(Major major) {
		// TODO Auto-generated method stub
		return getMajorDao().findMajorEtyByExmp(major);
	}

	@Override
	public void removeMajor(Major major){
		// TODO Auto-generated method stub
		getMajorDao().deleteMajorEtyByObj(major);
	}

	@Override
	public void removeAllMajor(int[] ids) {
		getMajorDao().deleteAllEty(ids);
		// TODO Auto-generated method stub
		
	}
	
	

	@Override
	public List<Major> importMajorList(File majorFile){
		  List<Major> majorList=getMajorListFromStr(importxlsxFile(majorFile));
		  List<Major> existMajorList=new ArrayList<Major>();		  
		 for(int i=0;i<majorList.size();i++){		
			 Major major=majorList.get(i);
			 if((getMajorDao().findMajorEtyByHql("from Major where majorId="+major.getMajorId()+"and academy.academyId="+major.getAcademy().getAcademyId())).size()>0)			 
				 existMajorList.add(major);		 
		 }
		     majorList.removeAll(existMajorList);
		   if(!majorList.isEmpty())
		    	getMajorDao().saveMajorEtyList(majorList);
		   System.out.println("大小："+majorList.size());
			return existMajorList;
		}			
		@Override
		public List getAcmyNameAndId(){
			return getAcademyDao().findAcmyNameAndId();
		}
		//解字符串
		 private  List<Major> getMajorListFromStr(String str){		
			List<Major> list=new ArrayList<Major>();
			String [] firstArray=str.split(";");
			for(int i=0;i<firstArray.length;i++){
				String [] secondArray=firstArray[i].split(":");
				Academy acmy=new Academy();
				acmy.setAcademyId(Integer.parseInt(secondArray[4]));
				//Academy acmy =(Academy)getAcademyDao().findAcademyEtyById(Integer.parseInt(secondArray[4])).get(0);
				list.add(new Major(Integer.parseInt(secondArray[0]),secondArray[1],secondArray[2],com.zq.it.tool.Constant.getDateFromStr(secondArray[3]),acmy));
			}
			//System.out.println("大小："+list.size());
			return list;
		}
		 
	//数据的导出
	@Override
		public ByteArrayInputStream exportMajorList(String[] headTitle,List list){
			return new ByteArrayInputStream(exportToExcel(list,headTitle,HeadTitle.EN_MAJORHEAD.getArray(),"com.zq.it.pojo.Major").toByteArray());
		}
		
}
